package com.openingedu.mba.screen.fragment;

import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

import android.content.Context;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.LinearLayout;
import android.widget.ListView;
import android.widget.TextView;

import com.actionbarsherlock.app.SherlockFragment;
import com.lurencun.android.support.widget.CommonAdapter;
import com.lurencun.android.toolkit.util.ActivitySwitcher;
import com.openingedu.mba.R;
import com.openingedu.mba.common.AppSetting;
import com.openingedu.mba.common.CommConsts;
import com.openingedu.mba.common.CommonUtil;
import com.openingedu.mba.entity.PaperVO;
import com.openingedu.mba.persist.DataHelper;
import com.openingedu.mba.persist.DbPaper;
import com.openingedu.mba.screen.activity.practice.CommonPraticeActivity;
import com.openingedu.mba.screen.adapter.LocalLogicAdapter;

/**
 * 本地逻辑试卷Tab
 * @author unidw
 *
 */
public class FragmentLocalTabLogic extends SherlockFragment {
	
	private Context mContext;
	private ListView listView;
	
	private boolean initialized = false;
	private DataHelper dataHelper;
	private List<PaperVO> paperList;
	private CommonAdapter<PaperVO> logicAdapter;
	
	@Override
	public View onCreateView(LayoutInflater inflater, ViewGroup container,
			Bundle savedInstanceState) {
		LinearLayout logicLayout = (LinearLayout)inflater.inflate(R.layout.ft_local_tab_logic, container, false);
		mContext = logicLayout.getContext();
		listView = (ListView) logicLayout.findViewById(R.id.list_container);
		TextView emptyView = (TextView)logicLayout.findViewById(R.id.listEmptyView);
		listView.setEmptyView(emptyView);
		logicAdapter = new LocalLogicAdapter(mContext, adapterHandler);
		if (!initialized) {
			dataHelper = new DataHelper(mContext);
			try {
				paperList = new ArrayList<PaperVO>();
				List<DbPaper> dbPaperList = dataHelper.getDbPaperDao().queryForAll();
				for (DbPaper dbPaper : dbPaperList) {
					paperList.add(CommonUtil.db2VO(dbPaper));
				}
				logicAdapter.updateDataCache(paperList);
				listView.setAdapter(logicAdapter);
			} catch (SQLException e) {
				e.printStackTrace();
			}
		}
		else {
			
		}
		return logicLayout;
	}
	
	// 接收adapter点击事件
	final Handler adapterHandler = new Handler() {
		@Override
		public void handleMessage(Message msg) {
			switch (msg.what) {
				// 练习
				case 0:
					PaperVO paper = (PaperVO) msg.obj;
					practicePaper(paper);
					break;
				default:
					break;
			}
		}
	};
	
	private void practicePaper(PaperVO paper) {
		String[] keys = new String[] { AppSetting.PRACTICE_WAY, CommConsts.Comm.PAPER_ID, AppSetting.MENU_TITLE };
		Object[] vals = new Object[] { CommConsts.Practice.PAPER_WAY, paper.getPaperID(), paper.getPaperTitle() };
		ActivitySwitcher.switchTo(getSherlockActivity(), CommonPraticeActivity.class, keys, vals);
	}
	
//	/**
//	 * 设置列表的EmptyView
//	 * @return EmptyView
//	 */
//	private View getEmptyView() {
//		TextView emptyView = new TextView(mContext);
//		emptyView.setLayoutParams(new LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.MATCH_PARENT));
//		emptyView.setText(getResources().getString(R.string.str_list_is_empty));
//		emptyView.setTextSize(20);
//		emptyView.setGravity(Gravity.CENTER_HORIZONTAL| Gravity.CENTER_VERTICAL);
//		emptyView.setVisibility(View.GONE);
//		((ViewGroup) listView.getParent()).addView(emptyView);
//		return emptyView;
//	}
	
}
